package com.service.impl;
import com.service.AggsService;
import com.utils.ConnectionUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.range.ParsedBinaryRange;
import org.elasticsearch.search.aggregations.bucket.range.ParsedRange;
import org.elasticsearch.search.aggregations.metrics.ParsedCardinality;
import org.elasticsearch.search.aggregations.metrics.ParsedExtendedStats;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

public class AggsServiceImpl implements AggsService {
    @Override
    public void cardinalityAggs(String index, String filed, String bieming) throws Exception {
//        RestHighLevelClient client = ConnectionUtils.getClient();
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        //设置聚合
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality(bieming).field(filed));
//        //创建查询的请求对象
//        SearchRequest request = new SearchRequest(index).source(searchSourceBuilder);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        SearchResponse response = baseAggs(index, AggregationBuilders.cardinality(bieming).field(filed));
        //获取聚合的结果,注意实际返回的类型应该是一个子类型,但是不确定是什么类型,可以通过debug看看是什么类型
        ParsedCardinality aggregation = response.getAggregations().get(bieming);
        System.err.println(aggregation.getValue());
    }

    @Override
    public void rangeAggs(String index, String bieming, AggregationBuilder aggregationBuilder) throws Exception {
//        RestHighLevelClient client = ConnectionUtils.getClient();
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        //设置聚合
//        searchSourceBuilder.aggregation(aggregationBuilder);
//        //创建查询的请求对象
//        SearchRequest request = new SearchRequest(index).source(searchSourceBuilder);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchResponse response = baseAggs(index, aggregationBuilder);
        ParsedRange aggregations = response.getAggregations().get(bieming);
        aggregations.getBuckets().forEach(bucket->{
            Object key = bucket.getKey();
            long docCount = bucket.getDocCount();
            System.err.println("key--->" + key + "---doccount===>" + docCount);
        });
    }

    @Override
    public void IpAggs(String index, String bieming, AggregationBuilder aggregationBuilder) throws Exception {
        SearchResponse response = baseAggs(index, aggregationBuilder);
        //ip范围查询
        ParsedBinaryRange aggregations = response.getAggregations().get(bieming);
        aggregations.getBuckets().forEach(bucket->{
            Object key = bucket.getKey();
            long docCount = bucket.getDocCount();
            System.err.println("key--->" + key + "----doccount===>" + docCount);
        });
    }

    @Override
    public void extendedStatsAggs(String index, String filed, String bieming) throws Exception {
        SearchResponse response = baseAggs(index, AggregationBuilders.extendedStats(bieming).field(filed));
        ParsedExtendedStats aggregations = response.getAggregations().get(bieming);
        System.err.println(aggregations.getAvg());
        System.err.println(aggregations.getCount());
        System.err.println(aggregations.getMax());
    }


    public SearchResponse baseAggs(String index, AggregationBuilder aggregationBuilder) throws IOException {
        RestHighLevelClient client = ConnectionUtils.getClient();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置聚合
        searchSourceBuilder.aggregation(aggregationBuilder);
        //创建查询的请求对象
        SearchRequest request = new SearchRequest(index).source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        return response;
    }
}




















